<?php

/**
 * Description of Auth
 *
 * @author markos
 */
class VirtualLabs_Login {

    public function __construct() {
        
    }

    public function doLogin($username, $password) {
        $debugLog = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
        $debugLog->debug("[ authenticating ] username: $username password: $password");
        if (trim($username) != "") {
            try {
                $authentication = new VirtualLabs_Auth($username, $password);
                if ($authentication->authenticate()) {
                    return true;
                }
            } catch (Exception $e) {
                throw $e;
            }
        }
        return false;
    }

    public function loginForgot($key) {
        $userDao = new VirtualLabs_Admin_Usuario();
        $user = $userDao->loginForgotUsuario($key);

        if ($user) {
            $generatedPassword = $userDao->generatePassword();
            $this->_debugLogger->debug('[generatedPassword] : ' . $generatedPassword);
            if ($generatedPassword) {
// email support password:  AwWmJc]U))UF
                $config = array('auth' => 'login',
                    'port' => '26',
                    'username' => 'support@pearljamparaguay.com',
                    'password' => 'AwWmJc]U))UF');
                $tr = new Zend_Mail_Transport_Smtp('mail.pearljamparaguay.com', $config);
                Zend_Mail::setDefaultTransport($tr);
                Zend_Mail::setDefaultFrom('support@pearljamparaguay.com', 'Soporte Virtual Labs Manager');
                Zend_Mail::setDefaultReplyTo('support@pearljamparaguay.com', 'Soporte Virtual Labs Manager');
                $mail = new Zend_Mail();
                $mail->addTo($user['email']);
                $this->_debugLogger->debug('[email] : ' . $user['email']);
                $mail->setSubject('Solicitud de cambio de password');
                $msg = utf8_encode("Estimado/a " . $user["nombre"] . ": <br><br>Clic <a href='" .
                        $this->view->baseUrl() . "/academico/login/resetpassword/password/" .
                        $generatedPassword .
                        "/username/" . $user['username'] . "'>aquí</a> para completar el proceso de reinicio de contraseña. <br>"
                        . "Si el enlace no funciona, utilice estos datos para ingresar:
                                        <ul><li>username : {$user['username']}</li><li>password: {$generatedPassword}</li></ul><br><br>Saludos!");
                $mail->setBodyHtml($msg);
                $mail->send($tr);
                return "<div class='alert alert-block alert-success fade in' id='value-alert'>Password restablecido. Favor verificar su correo!</div>";
            } else {
                return "<div class='alert alert-block alert-error fade in' id='value-alert'>Contacte con el administrador!</div>";
            }
        } else {
            return "<div class='alert alert-block alert-error fade in' id='value-alert'>Contacte con el administrador!</div>";
        }
    }

}

?>
